// pages/dynamic/dynamic.js
let axios = require("../../utils/util.js")
const app = getApp()
const myaudio = wx.createInnerAudioContext() //音频
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Sum_: 0, //未读信息 
    statusBarHeight: '44',
    isStart: null, //播放下标
    iscur: false, //播放状态
    options: [{
        name: '微信',
        icon: 'wechat',
        openType: 'share'
      },
      {
        name: '朋友圈',
        icon: 'wechat-moments',
        description: "右上角分享到朋友圈"
      },
    ],
    showShare: false, //分享面板
    dy_userId: '', //当前选中人id
    recordId: '', //动态id
    user_list: {}, //我的信息
    reportshow: false, //举报动态popup
    page: 1,
    loading: false,
    collectRoom_data: true, //下拉默认还有数据
    imgList: [], //图片预览全集
    voide_src: '',
    voice_model: false,
    show: false,
    poabsolute: 28,
    mesChangeNum: 1,
    messageTop: ["关注", "推荐", "精选"],
    tab_xinbie: false,
    cur_tab: 0,
    left_Gender: [{
        id: 0,
        icon: "https://img.xunyinjiaoyou.com/static/xiaochengxu/img_05/dynamicpage_icon_nannv_nor@2x.png",
        text: '全部'
      },
      {
        id: 1,
        icon: "https://img.xunyinjiaoyou.com/static/xiaochengxu/img_05/dynamicpage_icon_nan_nor@2x.png",
        text: '男生'
      },
      {
        id: 2,
        icon: "https://img.xunyinjiaoyou.com/static/xiaochengxu/img_05/dynamicpage_icon_nv_nor@2x.png",
        text: '女生'
      },
    ],
    topList: [],
    recordList: [],
  },
  tosystemMes() {
    wx.navigateTo({
      url: '/pages/systemMes/systemMes'
    })
  },
  //语音播放
  start(data) {
    let that = this;
    let e = data.currentTarget.dataset.start[0]; //播放地址
    console.log(e);
    let e2 = data.currentTarget.dataset.start[1]; //第几个下标
    that.setData({
      iscur: true
    })
    console.log(that.data.isStart);
    console.log(e2);
    if (that.data.isStart != e2) { //播放中
      console.log("播放");
      that.setData({
        isStart: e2
      })
      myaudio.src = e;
      myaudio.play(); //开始播放 
    } else { //暂停播放
      console.log("暂停");
      myaudio.pause()
    }
  },
  //分享朋友圈和微信
  shar_model() {
    this.setData({
      showShare: true
    })
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },
  // 动态点赞
  likeRecord(data) {
    let recordId = data.currentTarget.dataset.recordid[0];
    let index = data.currentTarget.dataset.recordid[1];
    console.log(index);
    let params = {
      recordId: recordId
    };
    axios.axios({
      url: "/record/likeRecord",
      data: params
    }).then((res) => {
      if (res.data.status == 0) {
        this.setData({
          ['recordList[' + index + '].isLiked']: res.data.result.type == 1 ? '1' : '0',
          ['recordList[' + index + '].likeCount']: res.data.result.likeList.length,
        })
        wx.showToast({
          title: res.data.text,
          icon: "none",
          duration: 2000
        })
      }
    })
  },
  toDetails(data) {
    wx.navigateTo({
      url: '/pages/dynamic/Details?recordId=' + data.currentTarget.dataset.recordid,
    })
  },
  onCloseShare() {
    this.setData({
      showShare: false
    })
  },

  onSelect(event) {
    // console.log(event);
  },
  onClickHide() {
    this.setData({
      tab_xinbie: false
    });
  },

  tab_xinbie_true() {
    this.setData({
      tab_xinbie: !this.data.tab_xinbie
    })
  },
  qie_cur_tab(data) {
    this.setData({
      cur_tab: data.currentTarget.dataset.index,
      tab_xinbie: false
    })
    this.dync_tab(this.data.mesChangeNum)
  },
  mesChange(data) {
    let index;
    if (data.currentTarget) {
      index = data.currentTarget.dataset.index;
    } else {
      index = data;
    }
    this.setData({
      poabsolute: 28 + index * 108,
    });
    switch (index) {
      case 0:
        this.setData({
          type: 2,
        })
        this.dync_tab(index)
        break;
      case 1:
        this.setData({
          type: 3,
        })
        this.dync_tab(index)
        break;
      case 2:
        this.setData({
          type: 1,
        })
        this.dync_tab(index)
        break;
    }
  },
  dync_tab(index) {
    let params = {
      page: 1,
      type: this.data.type,
      topicId: '',
      sex: this.data.cur_tab
    };
    axios.axios({
      url: "/record/recordList",
      data: params
    }).then((res) => {
      if (res.data.status == 0) {
        res.data.result.recordList.forEach((item, index, arr) => {
          if (item.picUrl) {
            arr[index].picUrl = arr[index].picUrl.split(';');
          }
        })
        this.setData({
          mesChangeNum: index,
          topList: res.data.result.topList, //热度话题
          recordList: res.data.result.recordList, //动态
        })

      }
    })
  },
  play_voice_click(data) {
    let voide_src = data.currentTarget.dataset.src;
    this.setData({
      voice_model: true,
      voide_src: voide_src,
    })
  },
  tuichu_play(event) {
    this.setData({
      voide_src: '',
      voice_model: false,
    })
    if (!event.detail.fullScreen) {
      // 退出全屏
      console.log("退出全屏");
      this.videoContext.exitFullScreen()
    } else {
      // 全屏
      console.log("全屏");
      this.videoContext = wx.createVideoContext('video');
      this.videoContext.requestFullScreen({
        direction: 0
      });
    }
  },
  // 放大背景图
  preview(event) {
    this.setData({
      imgList: []
    })
    event.currentTarget.dataset.src[0].forEach(item => {
      this.data.imgList.push(item);
    });
    let currentUrl = event.currentTarget.dataset.src[1]
    wx.previewImage({
      current: currentUrl, // 当前显示图片的http链接
      urls: this.data.imgList // 需要预览的图片http链接列表
    })
  },
  to_Topics(data) {
    var nowdata = data.currentTarget.dataset.item
    //将对象转为string
    var queryBean = JSON.stringify(nowdata)
    wx.navigateTo({
      url: '/pages/dynamic/topics?queryBean=' + queryBean,
    })
  },
  to_release() {
    wx.navigateTo({
      url: '/pages/dynamic/release',
    })
  },
  toReporting() {
    wx.showToast({
      title: '暂不支持',
      icon: 'none',
      duration: 2000,
    })
    // wx.navigateTo({
    //   url: '/pages/me/me?memberId=' + data.currentTarget.dataset.perfectnumber,
    // })
  },
  // 删除动态
  delRecord() {
    let params = {
      recordId: this.data.recordId
    };
    axios.axios({
      url: "/record/delRecord",
      data: params
    }).then((res) => {
      wx.showToast({
        title: res.data.text,
        icon: 'none',
        duration: 2000
      })
      if (res.data.status == 0) {
        this.setData({
          reportshow: false
        })
        this.mesChange(this.data.mesChangeNum)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let user_list = wx.getStorageSync('UserInformation')
    if (user_list) {
      app.globalData.userInfo = user_list
      app.globalData.user_id = user_list.userId;
      app.globalData.userToken = user_list.token;
      app.globalData.perfectNumber = user_list.perfectNumber;
      this.setData({
        user_list: user_list,
        // Sum_:app.globalData.Sum_,
      })
      // console.log(app.globalData.Sum_);
      this.mesChange(this.data.mesChangeNum)

      myaudio.onPlay(() => { //开始播放
        console.log("开始的回调");
      })
      myaudio.onPause((res) => { //暂停播放
        console.log('暂停播放回调!');
        that.setData({
          isStart: null
        })
      })
      myaudio.onEnded((res) => { //结束播放
        console.log("结束的回调");
        that.setData({
          isStart: null,
          iscur: false
        })
        // myaudio.stop()
      })
      wx.getSystemInfo({
        success: res => {
          that.setData({
            statusBarHeight: res.statusBarHeight,
          })
        },
      });
    } else {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      })
    }
  },
  // 跳转个人主页
  tomemberId(data) {
    wx.navigateTo({
      url: '/pages/me/me?memberId=' + data.currentTarget.dataset.perfectnumber,
    })
  },
  call(data) {
    let recordId = data.currentTarget.dataset.recordid;
    console.log(recordId);
    let params = {
      recordId: recordId
    };
    axios.axios({
      url: "/record/call",
      data: params
    }).then((res) => {
      wx.showToast({
        title: res.data.text,
        icon: 'none',
        duration: 3000
      })
    })
  },
  onClose() {
    this.setData({
      reportshow: false
    });
  },
  showpopup(data) {
    let dy_userId = data.currentTarget.dataset.userid[0];
    let recordId = data.currentTarget.dataset.userid[1];
    this.setData({
      reportshow: true,
      dy_userId: dy_userId,
      recordId: recordId
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.mesChange(this.data.mesChangeNum);
    setTimeout(() => {
      wx.stopPullDownRefresh(); //停止当前页面下拉刷新
      return wx.showToast({
        title: "加载成功",
        icon: 'none',
        duration: 2000
      })
    }, 1500)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.collectRoom_data) {
      this.data.page++
      this.setData({
        page: this.data.page,
        loading: true,
      })
      this.bottomrecordList()
    }
  },
  bottomrecordList() {
    let params = {
      page: this.data.page,
      type: this.data.type,
      topicId: '',
      sex: this.data.cur_tab
    };
    axios.axios({
      url: "/record/recordList",
      data: params
    }).then((res) => {
      if (res.data.status == 0) {
        if (res.data.result.recordList.length > 0) {
          res.data.result.recordList.forEach((item, index, arr) => {
            if (item.picUrl) {
              arr[index].picUrl = arr[index].picUrl.split(';');
            }
          })
          let a = this.data.recordList;
          let b = res.data.result.recordList
          let c = a.concat(b);
          setTimeout(() => {
            this.setData({
              topList: res.data.result.topList, //热度话题
              collectRoom_data: true,
              loading: false,
              recordList: c,
            })
          }, 1500)
        } else {
          this.setData({
            loading: false,
            collectRoom_data: false,
          })
          wx.showToast({
            title: "没有更多",
            icon: 'none',
            duration: 2000 //持续的时间
          })
        }
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
